<template>
  <view
    class="content"
    :style="{
      'padding-top': showHead ? height + 'px' : 0,
      background: showHead ? '' : 'none',
    }"
  >
    <view
      v-show="showHead"
      :style="{
        height: statusBarHeight + 'px',
      }"
    >
    </view>
    <view
      v-show="showHead"
      class="head"
      :style="{
        top: statusBarHeight + 'px',
      }"
    >
      <view class="head_left">
        <u-icon
          name="arrow-left"
          color="#FFFFFF"
          size="24"
          :bold="true"
          @click="toBack"
        ></u-icon>
      </view>
      <view class="text">已购课程</view>
    </view>
    <scroll-view
      class="box"
      :style="{
        height: showHead ? scrollHeight + 'px' : fatherHeight + 'px',
      }"
      scroll-y="true"
      @scroll="scroll"
      @scrolltolower="scrolltolower"
      v-if="keList"
    >
      <view v-for="(val, index) in keList.data" :key="index">
        <view class="radio_view">
          <view class="radio_left">
            <view
              v-show="!flag"
              class="item_left_clickView"
              @click="change(val)"
            >
              <image
                v-if="!val.clickable"
                class="item_left_click"
                src="../../static/images/my/icon_grzy_quanxuan.png"
                mode=""
              ></image>
              <image
                v-else
                class="item_left_clickable"
                src="../../static/images/my/icon_grzy_xc_xuanzhong.png"
                mode=""
              ></image>
            </view>
          </view>
          <view class="radio_right">
            <u-collapse :border="false">
              <u-collapse-item title="文档指南" name="Docs guide">
                <view
                  slot="title"
                  class="u-page__item__title__slot-title head1"
                >
                  <view class="head1_left">
                    <view class="title1">
                      {{ val.name || val.msg }}
                    </view>
                    <!-- <view class="text1"> #人物 </view> -->
                  </view>
                </view>
                <!-- <text class="u-collapse-content">涵盖uniapp各个方面，给开发者方向指导和设计理念，让您茅塞顿开，一马平川</text> -->
                <view
                  style="padding-bottom: 24rpx"
                  v-for="(item, index) in val.kechengInfo"
                  @click="toDetail(item, val)"
                >
                  <!-- <view
                    class="box_item"
                    :style="{
                      background: bacMapping[item.live_id - 1].background,
                      'border-radius': '16rpx',
                    }"
                  > -->
                  <view
                    class="box_item"
                    :style="{
                      background: 'rgba(39, 39, 39, 0.5)',
                      // background: bacMapping[item.see_ask].background,
                      'border-radius': '16rpx',
                    }"
                  >
                    <!-- <text>1 巴蜀人物概述</text>
					  		<view class="">
					  			
					  		</view> -->
                    <view class="itme_left">
                      <text class="left_content1">{{ item.title }}</text>
                      <!-- <view
                        class="left_type"
                        :class="'type_' + item.kecheng_type"
                      >
                        {{
                          item.kecheng_type == 0
                            ? "图文"
                            : item.kecheng_type == 1
                            ? "视频"
                            : "音频"
                        }}
                      </view> -->
                    </view>
                    <!-- <view class="item_right right_type_1">
                      {{ item.see_name || "免费" }}
                    </view> -->
                  </view>
                </view>
              </u-collapse-item>
            </u-collapse>
          </view>
        </view>
      </view>
      <view class="empty_view" v-if="keList.data && keList.data.length == 0">
        <image
          class="item_left_clickable"
          src="../../static/images/my/kong.png"
          mode=""
        ></image>
        <view v-if="from == 'history'">您还没有历史记录~</view>
        <view v-else>当前没有相关数据，请查看其他分类或功能。</view>
      </view>
    </scroll-view>

    <view class="bottom" v-show="!flag">
      <view class="bottom_left">
        <view @click="changeClick">
          <image
            v-if="!clickableAll"
            class="item_left_click"
            src="../../static/images/my/icon_grzy_quanxuan.png"
            mode=""
          ></image>
          <image
            v-else
            class="item_left_click"
            src="../../static/images/my/icon_grzy_xc_xuanzhong.png"
            mode=""
          ></image>
        </view>
        <view class="bottom_left_text">全选</view>
      </view>

      <view class="bottom_right" @click="deleteFn">删除</view>
    </view>

    <u-modal
      :show="show"
      :title="title"
      :content="content"
      confirmColor="#55B877"
      :closeOnClickOverlay="true"
      @confirm="confirm"
      @close="close"
      @cancel="cancel"
      :showCancelButton="true"
    ></u-modal>


    <floatingBall></floatingBall>
  </view>
</template>

<script>
import collect from "@/components/collect.vue"; // 收藏、
export default {
  components: {
    collect,
  },
  props: {
    flag: {
      type: Boolean,
      default: true,
    },
    showHead: {
      type: Boolean,
      default: true,
    },
    keChengList: {
      type: Object,
      default: () => {
        return {};
      },
    },
    from: {
      type: String,
      default: "",
    },
    fatherHeight: {
      type: Number,
      default: 0,
    },
  },
  watch: {
    keChengList: {
      deep: true, // 深度监听每一个属性值, 如果是对象可以深度监听
      immediate: true, // 进入页面后立即自动调用handler函数
      handler(newValue) {
        this.keList = newValue;
        if (this.keList.data) {
          for (let i = 0; i < this.keList.data.length; i++) {
            this.keList.data[i].clickable = false;
          }
        }

        console.log(this.keList, "newList111");
      },
    },
  },
  data() {
    return {
      statusBarHeight: 0,
      height: 0,
      studyList: [
        {
          studyStatus: 1,
          name: "巴蜀地区概述",
          renew: false,
          studyText: "已学习",
          clickable: false, // 当前是否选中
        },
        {
          studyStatus: 1,
          name: "巴蜀地区概述",
          renew: false,
          studyText: "继续学习",
          clickable: false, // 当前是否选中
        },
        {
          studyStatus: 2,
          name: "巴蜀地区概述",
          renew: false,
          studyText: "开始学习",
          clickable: false, // 当前是否选中
        },
      ],
      list: [
        {
          name: "巴蜀人物概述",
          type: 0,
          type_text: "图文",
          live_id: 2,
        },
        {
          name: "巴蜀人物概述",
          type: 1,
          type_text: "视频",
          live_id: 2,
        },
        {
          name: "巴蜀人物概述",
          type: 2,
          type_text: "音频",
          live_id: 3,
        },
        {
          name: "巴蜀人物概述",
          type: 2,
          type_text: "音频",
          live_id: 4,
        },
        {
          name: "巴蜀人物概述",
          type: 2,
          type_text: "音频",
          live_id: 5,
        },
        {
          name: "巴蜀人物概述",
          type: 2,
          type_text: "音频",
          live_id: 6,
        },
      ],
      bacMapping: [
        {
          id: 1,
          background: "rgba(39, 39, 39, 0.5)",
          text: "免费",
          color: "#fff",
        },
        {
          id: 2,
          background:
            "linear-gradient(to right, rgba(39, 39, 39, 0.5) 0%,rgba(39, 39, 39, 0.5) 60%, #8CD7A3 100%)",
          text: "元气驿站",
          color: "#fff",
        },
        {
          id: 3,
          background:
            "linear-gradient(to right, rgba(39, 39, 39, 0.5) 0%,rgba(39, 39, 39, 0.5) 60%, #FABC81 100%)",
          text: "启初梦谷",
          color: "#fff",
        },
        {
          id: 4,
          background:
            "linear-gradient(to right, rgba(39, 39, 39, 0.5) 0%,rgba(39, 39, 39, 0.5) 60%, #CFA5E3 100%)",
          text: "青空天台",
          color: "#fff",
        },
        {
          id: 5,
          background:
            "linear-gradient(to right, rgba(39, 39, 39, 0.5) 0%,rgba(39, 39, 39, 0.5) 60%,#7EC6FF 100%)",
          text: "无极云顶",
          color: "#fff",
        },
        {
          id: 6,
          background:
            "linear-gradient(to right, rgba(39, 39, 39, 0.5) 0%,rgba(39, 39, 39, 0.5) 60%, #6C87D0 100%)",
          text: "返朴归真",
          color: "#fff",
        },
      ],
      keList: {},
      scrollHeight: 0,
      clickableAll: false,
      show: false,
      title: "确认删除",
      content: "确认删除选中内容？",
      page: 1,
      last_page: 0,
    };
  },
  created() {
    const statusBarHeight = uni.getSystemInfoSync(); // 获取手机状态栏高度
    const windowHeihgt = statusBarHeight.windowHeight;
    this.statusBarHeight = statusBarHeight.statusBarHeight;
    this.$nextTick(() => {
      uni
        .createSelectorQuery()
        .select(".head")
        .boundingClientRect((data) => {
          this.height = data.height;
        })
        .exec();
    });

    if (!this.showHead) {
      // this.scrollHeight = windowHeihgt
      // console.log("走这里了1");
      this.height = 0;
    } else {
      // console.log("走这里了2");

      this.$nextTick(() => {
        const query = uni.createSelectorQuery().in(this);
        query.select(".head").fields({
          size: true,
        });

        query.exec((data) => {
          let sum = 0;
          data.forEach((item) => {
            sum += item.height;
          });
          this.scrollHeight = windowHeihgt - sum;
          if (!this.showHead) {
            this.scrollHeight = this.scrollHeight - 168;
          }

          console.log("各种", data);
        });
      });
    }

    // console.log(this.keChengList,'课程11111111111')
  },
  onLoad(options) {
    console.log(JSON.stringify(this.keChengList), "66666666");
    if (JSON.stringify(this.keChengList) != "{}") {
    } else {
      this.getList();
    }
  },
  mounted() {},
  methods: {
    scroll() {},
    scrolltolower() {
      if (this.page == this.last_page || this.last_page == 0) {
      } else {
        this.page++;
        this.getList();
      }
      // 判断是否是组件   如果是调用父组件分页
      if (JSON.stringify(this.keChengList) != "{}") {
        this.$emit('paging')
      }
      console.log(this.page,'子组件当前页')
      console.log(this.last_page,'子组件总页数')
      console.log("触发");
    },
    toBack() {
      uni.navigateBack({
        delta: 1,
      });
    },
    toDetail(item, val) {
      // console.log("kecheng_type", kecheng_type);
      uni.navigateTo({
        url: `/pages/bigResult/myResult_it?type=${val.type}&id=${item.id}`,
      });
      // if (kecheng_type == 0) {
      //   uni.navigateTo({
      //     url: "/pages/bigResult/myResult_it",
      //   });
      // } else if (kecheng_type == 2) {
      //   uni.navigateTo({
      //     url: "/pages/bigResult/myResult_au",
      //   });
      // }
    },
    change(item) {
      this.$forceUpdate();
      item.clickable = !item.clickable;
      // 判断是否全选
      let all = true;
      for (let i = 0; i < this.keList.data.length; i++) {
        if (this.keList.data[i].clickable == false) {
          all = false;
        }
      }
      if (all) {
        this.clickableAll = true;
      } else {
        this.clickableAll = false;
      }
    },
    changeClick() {
      this.clickableAll = !this.clickableAll;
      if (this.clickableAll) {
        for (let i = 0; i < this.keList.data.length; i++) {
          this.keList.data[i].clickable = true;
        }
      } else {
        for (let i = 0; i < this.keList.data.length; i++) {
          this.keList.data[i].clickable = false;
        }
      }
    },
    confirm() {
      let arr = [];
      let num = 0;
      for (let i = 0; i < this.keList.data.length; i++) {
        if (this.keList.data[i].clickable == true) {
          arr.push(this.keList.data[i].history_id);
          num++;
        }
      }
      console.log(arr, "数组");
      this.deletFn(arr);
    },
    close() {
      this.show = false;
    },
    cancel() {
      this.show = false;
    },
    // 删除按钮
    deleteFn() {
      let isTrue = true;
      for (let i = 0; i < this.keList.data.length; i++) {
        console.log(this.keList.data[i].clickable);
        if (this.keList.data[i].clickable == true) {
          isTrue = false;
        }
      }
      if (isTrue == true) {
        uni.$u.toast("您还未选择内容");
      } else {
        this.show = true;
      }
    },
    // 删除接口
    deletFn(arr) {
      this.$http
        .historyDel_his_kecheng({ ids: arr })
        .then((res) => {
          uni.$u.toast(res.message);
          this.show = false;
          setTimeout(() => {
            this.$emit("updateDataKecheng");
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    getList() {
      this.$http
        .orderGetKecheng({ page: this.page })
        .then((res) => {
          console.log(res.data.list, "已购课程");
          // this.detail = res.data.order;

          if (res.data.list.current_page == 1) {
            this.keList = res.data.list;
          } else {
            this.keList.data = this.keList.data.concat(res.data.list.data);
          }
          this.last_page = res.data.list.last_page;
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
  },
};
</script>

<style scoped lang="scss">
.content {
  box-sizing: border-box;
  background: url("/static/images/index.png") no-repeat;
  background-size: 100% 100%;
  position: fixed;
}

.head {
  width: 100%;
  padding: 18rpx 30rpx 47rpx;
  position: fixed;
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  z-index: 99;

  .head_left {
    position: absolute;
    left: 30rpx;
  }

  .head_right {
    position: absolute;
    right: 30rpx;
  }

  .text {
    font-size: 36rpx;
    font-family: PingFang SC-Bold, PingFang SC;
    font-weight: 400;
    color: #ffffff;
  }
}

.head1 {
  width: 100%;
  border-radius: 16rpx;
  padding: 8rpx 0;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .head1_left {
    .title1 {
      font-size: 32rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 400;
      color: #ffffff;
      display: flex;
      align-items: center;

      .renew_view {
        width: 16rpx;
        height: 16rpx;
        background: #ee6060;
        border-radius: 50%;
        margin-right: 16rpx;
      }

      .renew_text {
        font-size: 26rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #ee6060;
        margin-left: 16rpx;
      }
    }

    .text1 {
      font-size: 26rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 400;
      color: #adadad;
      margin-top: 16rpx;
    }
  }

  .head1_right0 {
    font-size: 28rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #888888;
  }

  .head1_right1 {
    font-size: 28rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #55b877;
  }

  .head1_right2 {
    font-size: 28rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #444444;
  }
}

/deep/ .u-cell--clickable {
  background: none;
}

/deep/ .u-collapse-item__content__text {
  padding: 0 !important;
}

/deep/ .u-collapse-item__content {
  margin-top: 24rpx;
  margin-bottom: 24rpx;
  padding: 0 !important;
  box-sizing: border-box;
}

/deep/.box {
  // width: 100%;
  // height: 100%;
  padding: 0 30rpx;
  box-sizing: border-box;
  // overflow-y: scroll;
  padding-top: 30rpx;
  .radio_view {
    display: flex;
    align-items: center;
    .radio_left {
      .item_left_clickView {
        position: relative;
        bottom: 20rpx;
        margin-right: 24rpx;
        .item_left_click {
          width: 40rpx;
          height: 40rpx;
        }
        .item_left_clickable {
          width: 40rpx;
          height: 40rpx;
        }
      }
    }

    .radio_right {
      flex: 1;
    }
  }
  .u-cell__body {
    background: rgba(39, 39, 39, 0.5) !important;
    border-radius: 16rpx;
  }
  .box_title {
    font-size: 34rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #444444;
    padding: 40rpx 0 32rpx;
  }

  .detail_item {
    width: 100%;
    padding: 32rpx 30rpx;
    box-sizing: border-box;
    display: flex;
    background: #f7f8fa;

    .detail_left {
      width: 210rpx;
      height: 210rpx;
      border-radius: 8rpx;

      image {
        width: 100%;
        height: 100%;
      }
    }

    .detail_right {
      flex: 1;
      padding: 16rpx 32rpx;
      box-sizing: border-box;

      .title {
        font-size: 32rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #444444;
      }

      .content_t {
        font-size: 28rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #adadad;
        margin-top: 16rpx;
      }

      .num {
        font-size: 26rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #888888;
        margin-top: 37rpx;
      }
    }
  }
}

.btn_box {
  width: 100%;
  height: 128rpx;
  position: fixed;
  bottom: 0;
  left: 0;
  padding: 16rpx 30rpx;
  box-sizing: border-box;

  .btn {
    height: 96rpx;
    background: linear-gradient(360deg, #50b674 0%, #aeddbd 100%);
    border-radius: 48rpx 48rpx 48rpx 48rpx;
    font-size: 30rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.box_item {
  width: 100%;
  background: rgba(39, 39, 39, 0.5);
  padding: 0 32rpx;
  height: 108rpx;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;

  // margin-top: 24rpx;
  .itme_left {
    font-size: 30rpx;
    font-family: PingFang SC-Medium, PingFang SC;
    color: #444444;
    display: flex;
    align-items: center;

    .left_content {
      font-size: 30rpx;
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      color: #adadad;
    }

    .left_content1 {
      font-size: 30rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 400;
      color: #ffffff;
    }

    .left_type {
      padding: 4rpx 10rpx;
      font-size: 24rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 400;
      margin-left: 16rpx;
      border-radius: 4rpx;
    }

    .type_0 {
      background: rgba(153, 187, 255, 0.302);
      color: #99bbff;
    }

    .type_1 {
      background: rgba(201, 153, 255, 0.302);
      color: #c999ff;
    }

    .type_2 {
      background: rgba(255, 182, 94, 0.302);
      color: #ffb65e;
    }
  }

  .item_right {
    font-size: 28rpx;
    font-family: PingFang SC-Medium, PingFang SC;
    // margin-right: 52rpx;
  }

  .right_type_0 {
    color: #55b877;
  }

  .right_type_1 {
    color: #fff;
  }
}

.bottom {
  width: 100%;
  height: 112rpx;
  background: #272727;
  position: fixed;
  bottom: 0;
  left: 0;
  padding: 16rpx 30rpx;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .bottom_left {
    display: flex;
    align-items: center;
    .item_left_click {
      width: 40rpx;
      height: 40rpx;
    }
    .bottom_left_text {
      margin-left: 16rpx;
      color: #eeeeee;
      font-size: 30rpx;
    }
  }

  .bottom_right {
    width: 250rpx;
    height: 80rpx;
    background: #ee6060;
    border-radius: 48rpx;
    text-align: center;
    line-height: 80rpx;
    font-size: 30rpx;
    color: #eeeeee;
  }
}
/deep/.u-modal__content__text {
  text-align: center;
}

.empty_view {
  margin-top: 300rpx;
  image {
    margin: 0 auto;
    width: 410rpx;
    height: 410rpx;
  }
  view {
    font-size: 30rpx;
    color: #d8d8d8;
    text-align: center;
  }
}
</style>